home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Ham Radio 2000
/
Ham Radio 2000.iso
/
ham2000
/
vhf
/
locator
/
loceng.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-10-17
|
3KB
|
111 lines
// ------------------------------------------------------------------------------------------------------------
// Distance and direction computation subprograms
// Code written 1984 - my second C program!
// only few modifications (new locs_to_bearing)
// made after that year
// ------------------------------------------------------------------------------------------------------------
#include "locowl.h"
#include <ctype.h>
#include <math.h>
#define PI 3.141592653589
struct location {
double longit;
double latit;
};
struct polar {
double distance;
double direction;
};
int locs_to_bearing (char far *from, char far *to,
double *distance, double *direction)
{
struct location from_spheric, to_spheric;
struct polar bearing;
int from_status, to_status;
int convert (char far *, struct location *);
int transform (struct location *, struct location *, struct polar *);
from_status = convert (from, &from_spheric);
to_status = convert (to, &to_spheric);
if (from_status+to_status)
return (from_status + 2 * to_status);
transform (&from_spheric, &to_spheric, &bearing);
*distance = bearing.distance;
*direction = 180.0 / PI * bearing.direction;
return 0;
}
static int convert(loc,place)
char far *loc;
struct location *place;
{
char mark;
int i;
int c;
char locator[6];
for ( i=0; i<=5; i++ ) {
locator[i] = tolower(loc[i]);
}
for ( i=0; i<=1; i++ ) {
if ((locator[i]<'a')||(locator[i]>'r')) return 1;
}
for ( i=2; i<=3; i++ ) {
if ( (locator[i]<'0')||(locator[i]>'9') ) return 1;
}
for ( i=4; i<=5; i++ ) {
if ( (locator[i]<'a')||(locator[i]>'x') ) return 1;
}
(place->latit) = 10*(locator[1] - 'a') - 90;
(place->latit) += locator[3] - '0';
(place->latit) += (locator[5] - 'a')/24.0 + 1/48.0;
(place->latit) *= PI/180.0;
(place->longit) = 20*(locator[0] - 'a') - 180;
(place->longit) += 2*(locator[2] - '0');
(place->longit) += (locator[4] - 'a')/12.0 + 1/24.0;
(place->longit) *= PI/180.0;
return 0;
}
static int transform(from,to,dd)
struct location *from, *to;
struct polar *dd;
{
double temp,nominator,radius;
radius = 180.0 /PI * (111.1451 + 0.56 * sin (to->latit + from->latit - PI/2.0));
(dd->distance) = radius * acos( temp = ( sin(to->latit) * sin(from->latit)
+ cos(to->latit) * cos(from->latit) * cos(to->longit - from->longit)) );
if (dd->distance < 0.1) {
dd->direction = 0;
}
else {
(dd->direction) = atan(( cos(from->latit)
* sin(to->longit - from->longit)
* cos(to->latit) )
/ ( nominator = ( sin(to->latit) - temp * sin(from->latit) ) ) );
if (nominator<0) dd->direction -= PI;
if (dd->direction<0) dd->direction += 2*PI;
}
return 0;
}